package com.example.leetdemo.finash;

import java.util.Stack;

public class Solution739 {

    public static int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack = new Stack<>();
        int[] result = new int[temperatures.length];
        stack.push(0);
        for (int i = 1; i < temperatures.length; i++) {
            if (temperatures[stack.peek()] < temperatures[i]) {
                while (stack.size() > 0 && temperatures[stack.peek()] < temperatures[i]) {
                    result[stack.peek()] = i - stack.peek();
                    stack.pop();
                }
            }
            stack.push(i);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] a = dailyTemperatures(new int[] {73,74,75,71,69,72,76,73});
        for (int i = 0; i < a.length; i++) {
            System.err.println(a[i]);
        }
    }
}
